<template >
    <!-- :style用来加背景图片 -->
    <div class = "login_container" :style="bg">
        <!-- 登录组件 -->
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
                <div class="text1" >
                    个人学习空间
                </div>
            </div>

            <!-- 表单 -->
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginformRules" label-width="80px" class="login_form">

                <el-form-item label="用户名" prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-key" type="password"></el-input>
                </el-form-item>

                <el-form-item label="初始等级" prop="initialLevel">
                    <el-select v-model="loginForm.initialLevel" placeholder="请选择">
                      <el-option label="有ACM,NIO,软件开发基础经验" value=1></el-option>
                      <el-option label="科班生" value=2></el-option>
                      <el-option label="跨专业学生" value=3></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="学习目的" prop="learnPurpose">
                    <el-select v-model="loginForm.learnPurpose" placeholder="请选择">
                      <el-option label="后端技术" value=1></el-option>
                      <el-option label="前端技术" value=2></el-option>
                      <el-option label="计算机基础" value=3></el-option>
                      <el-option label="技术手段" value=4></el-option>
                      <el-option label="其他" value=5></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="学习计划" prop="planning">
                    <el-select v-model="loginForm.planning" placeholder="请选择">
                        <el-option label="就业" value=2></el-option>
                        <el-option label="科研" value=1></el-option>
                        <el-option label="留学" value=3></el-option>
                        <el-option label="学习技能" value=4></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="学习方式" prop="learnStyle">
                    <el-select v-model="loginForm.learnStyle" placeholder="请选择">
                        <el-option label="喜欢看书自学" value=2></el-option>
                        <el-option label="喜欢动手实践" value=1></el-option>
                        <el-option label="喜欢上课交流学习或看视频" value=3></el-option>
                        <el-option label="其他" value=4></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item  class="btns">
                    <el-button type="primary" plain @click="register">注册并登录</el-button>
                    
                    
                    <el-button type="primary" plain @click="back">返回登录</el-button>
                    <el-button type="info" plain @click="resetLoginForm">重置 </el-button>
                </el-form-item> 

            </el-form>
            
            
        
        </div>
        <!-- 注册完成的弹窗 v-loading="loading" ref="partnerRef"-->
        <div>
            <el-dialog title="伙伴推荐"   :visible.sync="dialogFormVisible_tuijian"  @closed="handleClose">

                <h3 id="p_titie">
                    欢迎您加入学习空间，为您推荐以下学习伙伴
                    <br/>
                    <br/>
                    <hr/>
                </h3>
                <div id="p_text" v-loading.fullscreen.lock="fullscreenLoading"
                        element-loading-text="拼命加载中"
                        element-loading-spinner="el-icon-loading"
                        element-loading-background="rgba(0, 0, 0, 0.5)">
                    <div class="p_block" v-for="i in partner" :key="i" >
                        {{i}}
                    </div>               
                </div>
                
                <div slot="footer" class="dialog-footer">
                    <!-- <el-button @click="dialogFormVisible_tuijian = false">取 消</el-button> -->
                    <el-button type="primary" @click="turn_tishi">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <!-- 登录前提示的弹窗 -->
        <div>
            <el-dialog title="提示" :visible.sync="dialogFormVisible_tishi" 
            width="40%" @closed="login222">

                <div id="tishitext">
                    初次登陆，希望您先做一次学习风格测试
                </div>
                
                <div slot="footer" class="dialog-footer">
                    <!-- <el-button @click="dialogFormVisible_tuijian = false">取 消</el-button> -->
                    <el-button type="primary" @click="login222">确 定</el-button>
                </div>
            </el-dialog>
          </div>
    </div>

</template>

<script>
    // import Mock from './api/register.js';
    
    export default {
        data(){
            return {
                // 登录表单数据绑定对象
                loginForm:{
                    username:'',
                    password:'',
                    initialLevel:'',
                    learnPurpose:'',
                    planning:'',
                    learnStyle:'',
                    
                },
                loginForm111:{},

                // 表单验证规则对象
                loginformRules:{
                    username:[
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password:[
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ], 
                    initialLevel:[
                        { required: true, message: '请选择一项', trigger: 'change' },
                    ], 
                    learnPurpose:[
                        { required: true, message: '请选择一项', trigger: 'change' },
                    ], 
                    planning:[
                        { required: true, message: '请选择一项', trigger: 'change' },
                    ], 
                    learnStyle:[
                        { required: true, message: '请选择一项', trigger: 'change' },
                    ],
                },

                bg:{
                    backgroundImage: 'url('+require('../assets/sky.jpeg')+')',
                    backgroundRepeat: 'no-repeat',
                    backgroundSize:'100% 100%',
                    },
                dialogFormVisible_tuijian:false,
                dialogFormVisible_tishi:false,
                partner:[
                    // "sdfsdf",
                    // "sfgsdfsdfgf",
                ],

                fullscreenLoading:false,
            }
        },

        methods:{
            resetLoginForm(){
                this.$refs.loginFormRef.resetFields();
            },
            
            register(){
                // 转化数字
                // this.loginForm111=this.loginForm
                this.loginForm111 = {
                    username: this.loginForm.username,
                    password: this.loginForm.password
                }
                if(this.loginForm.initialLevel=="1") this.loginForm111.initialLevel=1
                if(this.loginForm.initialLevel=="2") this.loginForm111.initialLevel=2
                if(this.loginForm.initialLevel=="3") this.loginForm111.initialLevel=3

                if(this.loginForm.learnPurpose=="1") this.loginForm111.learnPurpose=1
                if(this.loginForm.learnPurpose=="2") this.loginForm111.learnPurpose=2
                if(this.loginForm.learnPurpose=="3") this.loginForm111.learnPurpose=3
                if(this.loginForm.learnPurpose=="4") this.loginForm111.learnPurpose=4
                if(this.loginForm.learnPurpose=="5") this.loginForm111.learnPurpose=5

                if(this.loginForm.planning=="1") this.loginForm111.planning=1
                if(this.loginForm.planning=="2") this.loginForm111.planning=2
                if(this.loginForm.planning=="3") this.loginForm111.planning=3
                if(this.loginForm.planning=="4") this.loginForm111.planning=4

                if(this.loginForm.learnStyle=="1") this.loginForm111.learnStyle=1
                if(this.loginForm.learnStyle=="2") this.loginForm111.learnStyle=2
                if(this.loginForm.learnStyle=="3") this.loginForm111.learnStyle=3
                if(this.loginForm.learnStyle=="4") this.loginForm111.learnStyle=4
                console.log(this.loginForm111)
                //   登录之前的预验证
                this.$refs.loginFormRef.validate(async valid =>{
                    // console.log(valid);
                    // 根据验证的值来确定是否发起网络请求
                    if(!valid) return;
                    // 请求服务器要保证api服务器处于运行中
                    // 返回数据只要data是真实的
                    //请求地址 与  请求参数
                    // 这是注册页面 ，login应修改为 register,
                    const { data:res} = await this.$http.post('register', this.loginForm111);
                    console.log(res);
                    // 状态码是200时成功，message弹框提示
                    if (res.status ==200) {
                        this.$message.success('注册成功！');
                        window.sessionStorage.setItem("token",res.data.token)
                        this.dialogFormVisible_tuijian=true
                        this.getPartnerInfo()
                        // 加载中
                        this.fullscreenLoading = true;
                        setTimeout(() => {
                        this.fullscreenLoading = false;
                        }, 10000);
                        // setTimeout(()=>{
                        //     // this.dialogFormVisible_tuijian=true
                        //     this.getPartnerInfo()
                        // }, 25000)
                    }
                        
                    else if (res.status =403)
                        return this.$message.error('用户名已存在，请更换用户名重新注册！');
                    else 
                        return this.$message.error('创建登录失败');
                    //将登录成功后的token，保存到客户端的session Storage中
                    // window.sessionStorage.setItem("token",res.data.token)
                //     //通过编程式导航跳转到后台主页，路由地址是/home
                    // this.$router.push("/Style");
                    window.sessionStorage.setItem("token",res.data.token)
                    window.sessionStorage.setItem("role",res.data.role)
                });
            },
            login(){
                //   登录之前的预验证
                this.$refs.loginFormRef.validate(async valid =>{
                    // console.log(valid);
                    // 根据验证的值来确定是否发起网络请求
                    if(!valid) return;
                    // 请求服务器要保证api服务器处于运行中
                    // 返回数据只要data是真实的
                    //请求地址 与  请求参数
                    const { data:res} = await this.$http.post('login', this.loginForm111);
                    console.log(res);
                    // 状态码是200时成功，message弹框提示
                    if (res.status !=200) 
                        return this.$message.error('登录失败！');
                    
                    //将登录成功后的token，保存到客户端的session Storage中
                    window.sessionStorage.setItem("token",res.data.token)
                    window.sessionStorage.setItem("role",res.data.role)
                //     //通过编程式导航跳转到后台主页，路由地址是/home
                    // this.$router.push("/Style");
                    

                });
            },
            login222(){
                this.$message.success('登录成功！');
                this.$router.push("/Style").catch((err)=>{console.log(err)})
            },
            back(){
                this.$router.push("/login");
            },
            turn_tishi(){
                this.dialogFormVisible_tuijian=false
                this.dialogFormVisible_tishi=true
            },

            async getPartnerInfo(){
            // get方法请求  地址 与 参数（参数最好定义到data中）
                const { data:res} = await this.$http.get('http://localhost:3000/learningSpace/table/firstRecommend')
                if(res.status !==200){
                    return this.$message.error('获取推荐伙伴信息失败'+res.message)
                }
                console.log(res)
                
                this.partner=res.data.slice(0,21)

            },
            handleClose () {
                this.dialogFormVisible_tuijian=false
            },
                
        }
    };
</script>

<style Lang= "less" scoped>
.login_container {
    height:100%;

}  

.login_box{
    width: 450px;
    height: 500px;
    background-color:#fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    opacity:0.87;
}

.text1{
    margin-top: 10px;
}

.avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
}
img{
    width: 100%;
    height:100%;
    border-radius: 50%;
    background-color: #eee ;
}

.btns{
    display: flex;
    justify-content:flex-end;
}

.login_form{
    position :absolute;
    bottom: 20px;
    width:100%;
    padding: 0 20px;
    box-sizing: border-box;

}
.p_kuang{
    position: absolute;
    top: 150px;
    left: 600px;
    background-color:#fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
    border-radius: 30px;
    height: 600px;
    width: 820px;
    opacity:0.9;
}
/* 水平布局 */
#p_text{
    margin-left: 50px;
    margin-right: 50px;
    margin-bottom: 50px;
    margin-top: 30px;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: flex-start;
}
/* 呈现每一个伙伴 */
.p_block{
    width: 240px;
    height: 50px;
    font-size: 20px;
    color: #000;
    text-align: center;

}
#p_titie{
    margin-top: 30px;
    margin-left: 50px;
    margin-right: 50px;
    margin-bottom: 30px;
    text-align: center;
    font-size: 30px;
    color: #000;
}
#tishitext{
    font-size: 20px;
    color: #000;
}
    
</style>